package com.cloudkeeper.leasing.identity.repository;

import com.cloudkeeper.leasing.base.repository.BaseRepository;
import com.cloudkeeper.leasing.identity.domain.VillageCadres;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 村干部管理 repository
 * @author cqh
 */
@Repository
public interface VillageCadresRepository extends BaseRepository<VillageCadres> {

    Long countAllByDistrictIdStartingWith(String districtId);

    Integer countAllByQuasiAssessmentRank(String QuasiAssessmentRank);

    List<VillageCadres> findAllByParentDistrictId(String objectId);

    List<VillageCadres> findAllByParentDistrictIdAndHasRetire(String objectId, String hasRetire);

    List<VillageCadres> findAllByQuasiAssessmentRankAndParentDistrictIdAndHasRetire(String rank, String parentDistrictId, String hasRetire);

    List<VillageCadres> findAllByQuasiAssessmentRankAndHasRetire(String rank, String hasRetire);


    @Modifying
    @Transactional
    @Query(value = "update village_cadres set state = '0'",nativeQuery = true)
    int updateState();

    @Modifying
    @Transactional
    @Query(value = "update village_cadres set state = '0' where cadresType = 'IN_SECRETARY'",nativeQuery = true)
    int updateStateByCadresType();

    // 更新素能评价字段
    @Modifying
    @Transactional
    @Query(value = "update village_cadres set evaluation = :evaluation where districtId = :districtId and id = :cadreId",nativeQuery = true)
    int updateEvaluation(@Param("evaluation") String evaluation, @Param("districtId") String districtId, @Param("cadreId") String cadreId);

    List<VillageCadres> findAllByHasRetireAndCadresType(String hasRetire, String cadresType);

    @Query(value = "SELECT a.districtId FROM ( SELECT * FROM SYS_District sd WHERE sd.districtLevel = 3 AND sd.districtType = 'Party' ) a " +
            "LEFT JOIN village_cadres vc ON vc.districtId = a.districtId AND vc.hasRetire = 0 AND vc.cadresType = 'SECRETARY' WHERE vc.name is null", nativeQuery = true)
    List<String> findVillageWithoutCadres();

}
